import 'package:enterprise_v6_app/app/components/custom_app_bar.dart';
import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/components/unit_selection/internal_unit/internal_unit_view.dart';
import 'package:enterprise_v6_app/app/components/unit_selection/unit_selection_controller.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/widget_extension.dart';
import 'package:enterprise_v6_app/app/data/entities/application/department_entity.dart';

///单位选择页面
class UnitSelectionPage extends StatelessWidget {
  UnitSelectionPage({super.key});

  final controller = Get.put(UnitSelectionController());

  ///部门列表
  Widget _getDepartmentList(int type) {
    return Container(
      padding: EdgeInsets.only(left: 16.w, right: 16.w, top: 10.w),
      alignment: Alignment.topCenter,
      color: const Color(0xffF5F5F6),
      child: CustomEasyRefreshList<DepartmentEntity>(
        isShowSearch: true,
        dataLoad: (pageIndex, pageSize, {String? search}) async {
          return controller.getDate(
            pageIndex,
            pageSize,
            type,
            search: search,
          );
        },
        itemBuilder: (DepartmentEntity item, int index) {
          return Container(
              padding: EdgeInsets.symmetric(horizontal: 12.w, vertical: 16.w),
              decoration: item.getIsChecked()
                  ? BoxDecoration(
                      borderRadius: BorderRadius.circular(8.w),
                      gradient: const LinearGradient(
                          //渐变位置
                          begin: Alignment.centerLeft,
                          end: Alignment.centerRight,
                          //渐变颜色[始点颜色, 结束颜色]
                          colors: [Color(0xff3B62FF), Color(0xff9FB2FF)]))
                  : BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(8.w)),
              child: Row(
                children: [
                  item.getIsChecked()
                      ? SvgPicture.asset(
                          AppImages.icon_unit_select,
                          width: 28.w,
                          height: 28.w,
                        )
                      : SvgPicture.asset(
                          AppImages.icon_unit_un_select,
                          width: 28.w,
                          height: 28.w,
                        ),
                  AppGaps.hGap12,
                  Text(
                    item.getShowName(),
                    style: TextStyle(
                        color: item.getIsChecked() ? const Color(0xffffffff) : const Color(0xff262F38),
                        height: 1.2,
                        fontWeight: FontWeight.w400,
                        fontSize: 16.sp),
                  ),
                ],
              )).clickOnce(() {
            // item.workDepartmentType = type;
            // controller.onTap(item);
          });
        },
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: CustomAppBar(
        titleText: '单位选择',
      ),
      body: GetBuilder(
          init: controller,
          builder: (_) {
            return DefaultTabController(
              length: 2,
              child: Column(
                children: [
                  Container(
                    margin: EdgeInsets.symmetric(horizontal: 50.w),
                    height: 54.w,
                    child: TabBar(
                      controller: controller.tabController,
                      tabs: const [
                        Tab(text: '内部单位'),
                        Tab(text: '第三方'),
                      ],
                      dividerColor: Colors.transparent,
                      labelPadding: EdgeInsets.only(bottom: 20.w),
                      labelStyle: TextStyle(
                        fontSize: 16.sp,
                        fontWeight: FontWeight.w500,
                      ),
                      unselectedLabelStyle: TextStyle(
                        fontSize: 16.sp,
                        fontWeight: FontWeight.w500,
                      ),
                      labelColor: Colors.black,
                      unselectedLabelColor: const Color(0xffA0A7B5),
                      indicatorPadding: EdgeInsets.only(top: 35.w, bottom: 10.w),
                      indicator: const BoxDecoration(
                        image: DecorationImage(
                          image: AssetImage(
                            AppImages.icon_app_work_plan,
                          ),
                        ),
                      ),
                    ),
                  ),
                  Expanded(
                    child: Container(
                      padding: EdgeInsets.only(bottom: 60.w),
                      color: const Color(0xffF5F5F6),
                      child: TabBarView(
                        controller: controller.tabController,
                        children: [
                          InternalUnitPage(
                            isRadio: controller.isRadio,
                          ),
                          _getDepartmentList(1),
                        ],
                      ),
                    ),
                  ),
                ],
              ),
            );
          }),
      bottomSheet: Container(
        height: 50.w,
        width: double.infinity,
        color: Colors.white,
        padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 5.w),
        child: ElevatedButton(
          style: ElevatedButton.styleFrom(
            backgroundColor: AppColors.blue_button,
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(22.r),
            ),
          ),
          onPressed: controller.complete,
          child: Text(
            '完成',
            style: TextStyle(
              fontSize: 16.sp,
              color: Colors.white,
            ),
          ),
        ),
      ),
    );
  }
}
